import { AllPage as pages, LoadFrom, CollectResume, DeleteResume as Delete } from '@/api/resume/resume'
import store from '@/store'
// import {resumecss, getHtmlContent} from '@/api/resume/export'

const state = {
  controllerName: 'resumeAll',
  dataList: {
    listLoading: false,
    list: [],
    departmentTree: [],
    salaryList: ['全部', '4k及以下', '4k-6k', '6k-8k', '8k-10k', '10k-12k', '12k及以上'],
    educationList: ['不限', '本科及以上', '硕士及以上', '博士/博士后', '大专及以上', '211/985', '统招', '海外留学'],
    addressList: ['全部', '北京', '上海', '广州', '深圳', '河北', '成都', '天津', '南京', '济南', '其他'],
    // ‘立即到岗’ ‘一个月到岗’‘三个月到岗’‘六个月到岗’
    sexList: ['不限', '男', '女'],
    jobStateList: [
      {
        value: '不限',
        label: '不限'
      },
      {
        value: '1',
        label: '立即到岗'
      },
      {
        value: '2',
        label: '一个月到岗'
      },
      {
        value: '3',
        label: '三个月到岗'
      },
      {
        value: '4',
        label: '六个月到岗'
      }
    ],
    searchForm: {
      LoginName: '',
      Address: '',
      SalaryRange: '',
      Education: '',
      JobState: '',
      Sex: -1,
      MinWorkHour: '',
      MaxWorkHour: '',
      MinAge: '',
      MaxAge: '',
      pageSize: 10,
      pageIndex: 1,
      dataCount: 0,
      pageCount: 0
    }
  },
  form: {
    formState: false,
    formDepartmentState: false,
    model: {},
    resState: false,
    importState: false
  }
}
const mutations = {
  pageSizeChange(state, par) {
    state.dataList.searchForm.pageSize = par
    this.dispatch(`resume/${state.controllerName}/findList`)
  },
  currentPageChange(state, par) {
    state.dataList.searchForm.pageIndex = par
    this.dispatch(`resume/${state.controllerName}/findList`)
  }
}

const actions = {
  delete(context, par) {
    console.log(par)
    global.tools.confirm(
      '确定要删除吗？',
      function() {
        Delete({
          id: par
        })
          .then(res => {
            const { Data } = res
            console.log(Data)
            global.tools.msg('删除成功!', '成功')
            context.dispatch('findList')
          })
          .catch(e => {
            console.log(e)
          })
      },
      null,
      '提示'
    )
  },
  loadNewForm(context) {
    var _state = context.state
    _state.form.model = {}
    _state.form.formState = true
  },
  // 加载表单
  loadForm(context, par) {
    var _state = context.state
    console.log('加载表单参数', par)
    var companyId = store.getters.companyId
    console.log(companyId, '公司id')
    par.CompanyId = companyId

    LoadFrom(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.model = Data
        _state.form.formState = true
      })
      .catch(error => {
        console.log(error)
      })
  },
  findList(context, par) {
    var _state = context.state
    // if (_state.dataList.searchForm.Address === '全部') {
    //   _state.dataList.searchForm.Address = ''
    // }
    // if (_state.dataList.searchForm.SalaryRange === '全部') {
    //   _state.dataList.searchForm.SalaryRange = ''
    // }
    // if (_state.dataList.searchForm.Education === '不限') {
    //   _state.dataList.searchForm.Education = ''
    // }
    // if (_state.dataList.searchForm.JobState === '不限') {
    //   _state.dataList.searchForm.JobState = ''
    // }
    if (_state.dataList.searchForm.MinWorkHour === '') {
      _state.dataList.searchForm.MinWorkHour = 0
    }
    if (_state.dataList.searchForm.MaxWorkHour === '') {
      _state.dataList.searchForm.MaxWorkHour = 0
    }
    if (_state.dataList.searchForm.MinAge === '') {
      _state.dataList.searchForm.MinAge = 0
    }
    if (_state.dataList.searchForm.MaxAge === '') {
      _state.dataList.searchForm.MaxAge = 0
    }
    _state.dataList.searchForm.MinWorkHour = parseInt(_state.dataList.searchForm.MinWorkHour)
    _state.dataList.searchForm.MaxWorkHour = parseInt(_state.dataList.searchForm.MaxWorkHour)
    _state.dataList.searchForm.MinAge = parseInt(_state.dataList.searchForm.MinAge)
    _state.dataList.searchForm.MaxAge = parseInt(_state.dataList.searchForm.MaxAge)
    _state.dataList.listLoading = true
    pages(_state.dataList.searchForm)
      .then(res => {
        const { Data } = res
        console.log(Data)
        _state.dataList.list = Data.List
        _state.dataList.searchForm.pageIndex = Data.PageIndex
        _state.dataList.searchForm.pageSize = Data.PageSize
        _state.dataList.searchForm.dataCount = Data.Count
        _state.dataList.searchForm.pageCount = Data.Pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  resumeCollect(context, par) {
    var _state = context.state
    console.log('收藏', par)
    CollectResume(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.resState = Data
        if (_state.form.resState) {
          global.tools.msg('操作成功!', '成功')
          _state.form.formState = false
          // context.dispatch('findList')
        } else {
          global.tools.msg('操作失败!', '错误')
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  handleSex(context, par) {
    console.log(par)
    var _state = context.state
    _state.dataList.searchForm.Sex = par
  },
  handleAddress(context, par) {
    console.log(par)
    var _state = context.state
    _state.dataList.searchForm.Address = par
  },
  handleEducation(context, par) {
    console.log(par)
    var _state = context.state
    _state.dataList.searchForm.Education = par
  },
  handleJobState(context, par) {
    console.log(par)
    var _state = context.state
    _state.dataList.searchForm.JobState = par
  },
  handleSalaryRange(context, par) {
    console.log(par)
    var _state = context.state
    _state.dataList.searchForm.SalaryRange = par
  },
  handleNodeClick(context, node) {
    var _state = context.state
    _state.form.model.departmentId = node.id
    _state.form.model.departmentName = node.name
    _state.form.formDepartmentState = false
  },
  handleDownload() {
    global.tools.msg('该功能需要成为会员才可以使用!', '警告')
  },
  handleSuccess(context, res) {
    console.log(res, 'handleSuccess')
    if (res.Status === 1) {
      var _state = context.state
      global.tools.msg('导入成功!', '成功')
      _state.form.importState = false
      context.dispatch('findList')
    } else {
      global.tools.msg('导入失败！', '提示')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
